import time

from selenium.webdriver.common.by import By

from common.basePage import Base
from page.page_login import Login


class DeviceModel(Base):
    # xpath定位
    LOCATE_ADD = '//span[@class="btn" and text()="添加"]'
    LOCATE_SELECT = '(//select[@id="device-type"])[1]'  # xpath定位索引从1开始
    LOCATE_DEVICE_MODEL = '//input[@id="device-model"]'
    LOCATE_MODEL_DESC = '//input[@id="device-model-desc"]'
    LOCATE_SUBMIT = '//span[@class="btn" and text()="确定"]'
    LOCATE_ALTER_SUBMIT = '//div/span[@class = "btn-no-border"and text()="确定"]'
    LOCATE_ALTER_SELECT = '(//select[@id="device-type"])[2]'
    LOCATE_ALTER_DEVICE = '(//input[@class="input-xl"])[3]'
    LOCATE_ALTER_DESC = '(//input[@class="input-xl"])[4]'

    # 定位设备型号页面
    def find_device_page(self):
        self.base_click(By.PARTIAL_LINK_TEXT, "设备型号")

    # 定位添加按钮
    def find_add_device(self):
        return self.base_find(locate=self.LOCATE_ADD)

    # 点击添加按钮
    def click_add_device(self):
        if self.find_add_device().text == "添加":  # 如果添加按钮的文本 == 添加时才点击
            self.base_click(locate=self.LOCATE_ADD)

    # 选中设备种类选择框的元素
    def select_device(self, locate, select_val):
        self.base_find_select(locate=locate, value=select_val)

    # 输入设备型号、型号描述
    def input_device_data(self, locate, model_val):
        self.base_send(locate=locate, value=model_val)

    # 清除设备型号、型号描述
    def clear_device_data(self, locate):
        self.base_send_clear(locate=locate)

    # 点击提交按钮
    def click_submit(self):
        self.base_click(locate=self.LOCATE_SUBMIT)

    # 获取第一页设备型号数据
    def get_first_page_device_mode(self):
        return self.base_get_first_page_device_mode(By.CSS_SELECTOR, ".field-value", 3)

    # 删除设备型号页面，第一项表单内容
    def del_frist_item(self):
        self.base_del_first_item(By.CSS_SELECTOR,
                                 ".result-list-item:first-child .result-list-item-btn-bar span:first-child")

    # 获取当前定位项的文本
    def get_device_text(self, by, locate):
        return self.base_get_text(by, locate)

    # 修改设备型号页面，第一项表单的内容。    a_or_w：a为追加写入，w重新写入
    def alter_first_item(self, select_val, model_val, desc_val, a_or_w: str):
        self.base_click(By.CSS_SELECTOR, ".result-list-item:first-child .result-list-item-btn-bar span:last-child")
        # 选中设备框
        self.select_device(self.LOCATE_ALTER_SELECT, select_val)
        if a_or_w == 'a':
            # 获取设备型号、型号描述表单的内容 + 追加写入的内容
            self.input_device_data(self.LOCATE_ALTER_DEVICE,
                                   self.get_device_text(by=By.XPATH, locate=self.LOCATE_ALTER_DEVICE) + model_val)
            self.input_device_data(self.LOCATE_ALTER_DESC,
                                   self.get_device_text(by=By.XPATH, locate=self.LOCATE_ALTER_DESC) + desc_val)
        else:
            # 先清除设备型号、型号描述的内容，再写入
            self.clear_device_data(self.LOCATE_ALTER_DEVICE)
            self.input_device_data(self.LOCATE_ALTER_DEVICE, model_val)
            self.clear_device_data(self.LOCATE_ALTER_DESC)
            self.input_device_data(self.LOCATE_ALTER_DESC, desc_val)
        # 点击确定，修改成功
        self.base_click(locate=self.LOCATE_ALTER_SUBMIT)
        return self

    # 添加设备
    def add_device(self, select_val, model_val, desc_val):
        # 点击添加，填入设备种类、设备型号、型号描述，点击确定
        self.click_add_device()
        self.select_device(self.LOCATE_SELECT, select_val)
        self.input_device_data(self.LOCATE_DEVICE_MODEL, model_val)
        self.input_device_data(self.LOCATE_MODEL_DESC, desc_val)
        self.click_submit()
        return self
